package net.hymnal.church;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;

public class HymnsManager {
	public enum HYMNS_TYPE {
		TYPE_NONE, TYPE_COMMON, TYPE_YOUNG
	};

	private static HYMNS_TYPE selectedType;
	static ArrayList<SingleHymn> m_arrayCommonHymns;
	static ArrayList<SingleHymn> m_arrayYoungHymns;

	
	
	static private int selectedCount;
	
	static public boolean isSelected() {
        return (selectedCount > 0);
	}
	
	static public void resetSelection() {
        selectedCount = 0;
	}
	
	
	static void selectHymnType(HYMNS_TYPE type){
		selectedType = type;
	}
	
	

	static void readYoungHymns(Activity a) {
		HymnsManager.m_arrayYoungHymns = new ArrayList<SingleHymn>();
		String fileName = Settings.youngHymnalPath;
		for (int i = 0; i < Settings.MAX_YOUNG_HYMNS; i++) {
			String appendStr = "";

			if (i < 100) {
				appendStr += "0";
			}

			if (i < 10) {
				appendStr += "0";
			}

			String fullName = fileName + appendStr + Integer.toString(i)
					+ ".txt";
			try {
				final InputStream is = a.getResources().getAssets()
						.open(fullName);
				if (is != null) {
					byte b_buffer[] = null;
					int fileSize = is.available();
					b_buffer = new byte[fileSize + 1];
					is.read(b_buffer);
					String text = new String(b_buffer, "Cp1251");
					SingleHymn singleHymn = new SingleHymn(i, text);
					HymnsManager.m_arrayYoungHymns.add(singleHymn);
				}
			} catch (IOException e) {
				Log.e("IO", "Young:File not found:" + i);
			}
		}
	}
	

	static void readCommonHymns(Activity a) {
		HymnsManager.m_arrayCommonHymns = new ArrayList<SingleHymn>();
		String FileName = Settings.commonHymnalPath + "r";
		for (int i = 0; i < Settings.MAX_COMMON_HYMNS; i++) {
			String fullName = FileName + Integer.toString(i) + ".txt";
			try {
				final InputStream is = a.getResources().getAssets()
						.open(fullName);
				if (is != null) {
					byte b_buffer[] = null;
					int fileSize = is.available();
					b_buffer = new byte[fileSize + 1];
					is.read(b_buffer);
					String text = new String(b_buffer, "Cp1251");
					SingleHymn singleHymn = new SingleHymn(i, text);
					HymnsManager.m_arrayCommonHymns.add(singleHymn);
				}
			} catch (IOException e) {
				System.out.println("read common hymns ex:" + e.getMessage());
			}
		}
	}
	

	static void readCommonMelodyTable(Activity a) {
		String fileName = Settings.tableCommonPath + Settings.tableMelodyFileName;
		HymnsManager.selectHymnType(HYMNS_TYPE.TYPE_COMMON);
		try {
			final InputStream is = a.getResources().getAssets().open(fileName);
			final InputStreamReader isr = new InputStreamReader(is);
			final BufferedReader br = new BufferedReader(isr);
			String line = null;
			while((line = br.readLine())!=null) {
				//line = br.readLine();
				String[] strSplit = line.split("==");
				SingleHymn h = null;
				for (int i = 0; i < strSplit.length; i++) {
					if (i == 0) {
						int hNum = Integer.parseInt(strSplit[0]);
						h = HymnsManager.getSingleHymn(hNum - 1);
					}
					if (i == 1) {
						// just skip, english number
					}
					if (i == 2 && h != null && (strSplit[i] != "null")) {
						// first line
						h.setFirstLine(strSplit[i]);
					}
					if (i == 3 && h != null && (strSplit[i] != "null")) {
						// filename
						h.setMelodyFileName(strSplit[i]);
					}
				}
				
				//if (strSplit.length == 4) {
				//	int hNum = Integer.parseInt(strSplit[0]);
				//	SingleHymn h = HymnsManager.getSingleHymn(hNum - 1);					
				//	h.setFirstLine(strSplit[2]);
				//	h.setMelodyFileName(strSplit[3]);
				//}
			}
			
		} catch (IOException e) { 
			System.out.println("No table for common_melydy hymns.");
		} finally {
			HymnsManager.selectHymnType(HYMNS_TYPE.TYPE_NONE);
		}
	}

	static int getNumberHymns() {
		if (selectedType == HYMNS_TYPE.TYPE_COMMON) {
			return HymnsManager.m_arrayCommonHymns.size();
		}
		if (selectedType == HymnsManager.HYMNS_TYPE.TYPE_YOUNG) {
			return HymnsManager.m_arrayYoungHymns.size();
		}
		return 0;
	}

	static SingleHymn getSingleHymn(int index) {
		if (HymnsManager.selectedType == HYMNS_TYPE.TYPE_COMMON) {
			return m_arrayCommonHymns.get(index);
		}
		if (HymnsManager.selectedType == HYMNS_TYPE.TYPE_YOUNG) {
			return m_arrayYoungHymns.get(index);
		}
		return null;
	}

	static boolean SelectHymns(String subString) {		
		selectedCount = 0;			
		int num = getNumberHymns();
		for (int i = 0; i < num; i++) {
			if (selectedType == HYMNS_TYPE.TYPE_COMMON) {
				SingleHymn h = m_arrayCommonHymns.get(i);
				h.checkTextAndSelect(subString);
				if (h.isSelected()) {
					selectedCount++;
				}
				continue;
			}
			if (selectedType == HYMNS_TYPE.TYPE_YOUNG) {
				SingleHymn h = m_arrayYoungHymns.get(i);
				h.checkTextAndSelect(subString);
				if (h.isSelected()) {
					selectedCount++;
				}
			}
			if (selectedType == HYMNS_TYPE.TYPE_NONE) {
				return false;
			}			
		}
		return (selectedCount > 0);		
	}
	
	static String[] getSelectedArray() {
		int k = 0;
		String[] array = new String[selectedCount];
		int num = getNumberHymns();
		for (int i = 0; i < num; i++) {
			SingleHymn h = getSingleHymn(i);
			if (h.isSelected()) {
				array[k] = Integer.toString(i + 1);
				String fLine = h.getFirstLine();
				if (fLine != null) {
					array[k] += " :" + fLine;
				}
				k++;
			}
		}		
		return array;
	}
}
